/************************************
 * Package: RGcpp					*
 * Authors: Rui Eduardo Gouveia Gil	*
 * Date: 2010						*
 ************************************/
#ifndef __RGCPP_DYNAMICSOCKET_H__
#define __RGCPP_DYNAMICSOCKET_H__

#ifdef _WIN32
#include <winsock2.h>
#endif
#include <openssl/ssl.h>
#include <openssl/err.h>

#include "rgcpp/net/ssl/SSLContext.h"
#include "rgcpp/net/sockets/TCPSocket.h"
#include "rgcpp/exceptions/IOException.h"
#include "rgcpp/exceptions/RuntimeException.h"

namespace rgcpp
{

class DynamicSocket : public TCPSocket
{
	private:
		SSL *ssl;
		bool sslMode;
		bool serverSide;
		SSLContext* sslContext;

		static std::string password2check;

	public:
		DynamicSocket(SSLContext* sslContext, const int socketFD, bool sslMode=false);
		DynamicSocket(SSLContext* sslContext, const IPAddress& ipAddress, bool sslMode=false);

		~DynamicSocket();

		int readData(char* buffer, const unsigned int size);
		void writeData(const char* buffer, const unsigned int size);

		int readEncryptedData(char* buffer, const unsigned int size);
		void writeEncryptedData(const char* buffer, const unsigned int size);

		void upgradeToSSL();
		void downgradeFromSSL();
		bool isOnSSLMode();

		bool isCertifiedConnection();
};

}

#endif
